<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h1>个人中心</h1>
    </div>
    <div class="content">
      <p>管理员ID：{{ userInfo.id }}</p>
      <el-divider></el-divider>
      <p>账号：{{ userInfo.account }}</p>
      <el-divider></el-divider>
      <p>用户组：{{ userInfo.userGroup }}</p>
      <el-divider></el-divider>
      <p>创建时间：{{ userInfo.ctime | formateDateYmd }}</p>
      <el-divider></el-divider>
      我的头像
      <el-upload
        class="avatar-uploader"
        :action="API + '/users/avatar_upload'"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="userInfo.imgUrl" :src="userInfo.imgUrl" class="avatar" />
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
        <el-button type="primary" @click="updateimg">修改头像</el-button>

    </div>
  </el-card>
</template>

<script>
import local from "@/utils/local";
import {updateUserImg} from '@/api/user'
export default {
  name: "Personal",
  data() {
    return {
      userInfo: "",
   newImg:'',
      API: "http://127.0.0.1:5000",
    };
  },
  methods: {
    handleAvatarSuccess(res, file) {
      let { code,imgUrl  } = res;
console.log("bbb",imgUrl);

      if (code === 0) {
        // 回填头像
        this.userInfo.imgUrl = this.API + imgUrl;
        this.newImg=imgUrl
      }
      console.log(res);
    },

    // 上传前触发  限制图片格式和大小
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }

      return isJPG;
    },
      async updateimg(){
    let res = await updateUserImg({imgUrl:this.newImg.substr(21)})
    console.log('ss',res);
    if(res.data.code==0){
      this.$bus.$emit('update')
    }
    
    

  },
  },

  created() {
    this.userInfo = local.get("userInfo");
    
  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  margin-top: 5px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>
